﻿using ImTools;
using Main.DataBaseContext;
using Main.dto;
using Main.enums;
using Microsoft.EntityFrameworkCore;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;

namespace Main.ViewModels
{
	public class AddToDoTaskDialogViewModel : BindableBase, IDialogAware
    {
        private readonly BusnessContext busnessContext;

        public ICommand AddToDoTask { get; set; }

        public ICommand Cancel { get; }

        private ToDoTask _toDoTask;

        public ToDoTask ToDoTask { get=>_toDoTask; set {

                _toDoTask = value;
                RaisePropertyChanged();
                RaisePropertyChanged(nameof(SelectedItem));
                //SetProperty(ref _toDoTask, value); 有坑，以后不用了
            } }

        public ToDoTaskStatus SelectedItem {

            get {
                if(ToDoTask is null){
                    return default;
                }
                return ToDoTaskStatusList.FindFirst(status=>status.Status == ToDoTask.Status);
            }

            set {
                ToDoTask.Status = value.Status;
                
                RaisePropertyChanged();
            }
            
        }

        public Boolean IsChecked { get; set; }

        public string ConfirmButtonContent => isNewAdd ? "添加" : "更新";


        public string Title => "添加待办";

        private Boolean isNewAdd;

        public List<ToDoTaskStatus> ToDoTaskStatusList { get; set; }

        public event Action<IDialogResult> RequestClose;

        public AddToDoTaskDialogViewModel(BusnessContext busnessContext)
        {
            this.busnessContext = busnessContext;

            ToDoTaskStatusList = new List<ToDoTaskStatus>() { 
                new ToDoTaskStatus(){ 
                    Status=1,
                    Detail="待办"
                },
                new ToDoTaskStatus(){ 
                    Status=2,
                    Detail = "已办"
                }
            };

            AddToDoTask = new DelegateCommand<object>(AddToDoTaskHandler);

            Cancel = new DelegateCommand(CancelHandler);
        }

        private async void CancelHandler()
        {

            RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));

           await busnessContext.ToDoTasks.LoadAsync();
        }

        private async void AddToDoTaskHandler(object arg) {

            if ( string.IsNullOrEmpty(ToDoTask.TaskSummary)) {
                MessageBox.Show("待办事项摘要不能为空");
                return;
            }

            if (isNewAdd) { 
                

                busnessContext.ToDoTasks.Add(ToDoTask);
            }
            
            await busnessContext.SaveChangesAsync();

            RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            if (parameters.TryGetValue("ExistToDoTask", out ToDoTask toDoTask))
            {

                this.ToDoTask = toDoTask;
                isNewAdd = false;
                
            }
            else {
                this.ToDoTask = new ToDoTask();
                isNewAdd = true;
            }
        }
    }
}
